package com.yn.club.subject.application.controller;



import com.yn.club.subject.domain.entity.SubjectBriefDO;
import com.yn.club.subject.application.dto.SubjectBriefDTO;
import com.yn.club.subject.domain.service.SubjectBriefDomainService;
import org.springframework.web.bind.annotation.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import javax.validation.constraints.NotEmpty;
import java.io.Serializable;
import java.util.List;
import com.yn.club.common.util.R;
import com.yn.club.common.util.PageReq;
import com.yn.club.common.util.PageResp;
import com.yn.club.subject.application.convert.SubjectBriefApplicationConverter;
import com.yn.club.subject.application.request.SubjectBriefQueryRequest;
import com.yn.club.subject.application.request.SubjectBriefSaveRequest;

/**
 * 简答题(SubjectBrief)表控制层
 *
 * @author YN
 * @since 2025-01-21 17:40:47
 */
@Slf4j
@RequiredArgsConstructor
@Validated
@RestController
@RequestMapping("subjectBrief")
public class SubjectBriefController {
    /**
     * 服务对象
     */
    private final SubjectBriefDomainService subjectBriefDomainService;
    private final SubjectBriefApplicationConverter subjectBriefApplicationConverter;

    /**
     * 分页查询数据
     *
     * @param pageReq 分页参数
     * @param query   查询条件
     * @return 所有数据
     */
    @GetMapping
    public R<PageResp<SubjectBriefDTO>> queryPage(PageReq pageReq, SubjectBriefQueryRequest query) {
        SubjectBriefDO subjectBriefDO = subjectBriefApplicationConverter.convertQueryRequestToDo(query);
        PageResp<SubjectBriefDO> page = subjectBriefDomainService.page(pageReq, subjectBriefDO);
        
        List<SubjectBriefDTO> subjectBriefDTOS = subjectBriefApplicationConverter.convertDoToDto(page.getList());
        return R.ok(PageResp.of(page,subjectBriefDTOS));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public R<SubjectBriefDTO> selectOne(@PathVariable Serializable id) {
    SubjectBriefDTO subjectBriefDTO = subjectBriefApplicationConverter.convertDoToDto(
      this.subjectBriefDomainService.getById(id));
     
    return R.ok(subjectBriefDTO);
    }

    /**
     * 新增数据
     *
     * @param subjectBrief 实体对象
     * @return 受影响条数
     */
    @PostMapping
    public R<Boolean> insert(@RequestBody SubjectBriefSaveRequest subjectBrief) {
       SubjectBriefDO subjectBriefDO = subjectBriefApplicationConverter.convertSaveRequestToDo(subjectBrief);
        return R.ok(this.subjectBriefDomainService.insert(subjectBriefDO));
    }

    /**
     * 修改数据
     *
     * @param subjectBrief 实体对象
     * @return 受影响条数
     */
    @PutMapping
    public R<Boolean> update(@RequestBody SubjectBriefSaveRequest subjectBrief) {
        SubjectBriefDO subjectBriefDO = subjectBriefApplicationConverter.convertSaveRequestToDo(subjectBrief);
        return R.ok(this.subjectBriefDomainService.updateById(subjectBriefDO));
    }

    /**
     * 删除数据
     *
     * @param ids 主键结合
     * @return 受影响条数
     */
    @DeleteMapping
    public R<Boolean> delete(@RequestBody @NotEmpty List<Integer> ids) {
        return R.ok(this.subjectBriefDomainService.delete(ids));
    }
}

